//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2013 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_FUNCTORS_PREDICATES_CHAR_H_
#define _BUOLA_FUNCTORS_PREDICATES_CHAR_H_

#include <buola/functors/predicates/unary.h>
#include <buola/utility/ulocale.h>
#include <unicode/uchar.h>

namespace buola { namespace fn {

///\addtogroup functors
///@{

///\name Character classification predicates
//@{

/////////////////////////////////////////////////////////////////////////////////////////////////
///
/// ctype-check predicate
///
/// Evaluated with a single argument, returns true if the parameter belongs to one of the ctypes
/// specified in the constructor, for the given locale
///
/// This class caches the ctype facets for the given locale, both for char and wchar_t, and can
/// thus be used with any of those types. The rationale for that is that obtaining the facet
/// from the locale is a much more expensive operation than actually using the facet to check
/// the type of a particular character.
///
/// The default copy constructor can be used since even that the locale object will be different,
/// the facets they point to will be identical.
///
/////////////////////////////////////////////////////////////////////////////////////////////////
struct AIsOfAsciiType : public GUnaryPredicate
{
    ///constructs the predicate given a ctype mask and a locale, which defaults to the current one
    explicit AIsOfAsciiType(int pType,const ULocale &pLocale=ULocale())
        :   mLocale(pLocale)
        ,   mType(pType)
    {}
    
    ///returns true if the character is of the given type
    bool operator()(wchar_t pCh) const
    {
        return u_hasBinaryProperty(pCh,(UProperty)mType);
    }
    
private:
    ULocale mLocale;
    int mType;
};

///returns a predicate that returns true if its argument is of the given character type
inline AIsOfType is_of_ctype(int pType,const ULocale &pLocale=ULocale())
{
    return AIsOfType(pType,pLocale);
}

///returns a predicate that returns true if its argument is alphanumeric
inline AIsOfType is_alnum(const ULocale &pLocale=ULocale())
{
    return AIsOfType(UCHAR_POSIX_ALNUM,pLocale);
}

///returns a predicate that returns true if its argument is a letter
inline AIsOfType is_alpha(const ULocale &pLocale=ULocale())
{
    return AIsOfType(UCHAR_ALPHABETIC,pLocale);
}

///returns a predicate that returns true if its argument is a digit
inline AIsOfType is_digit(const ULocale &pLocale=ULocale())
{
    return AIsOfType(UCHAR_NUMERIC_TYPE,pLocale);
}

///returns a predicate that returns true if its argument is a graphic character
inline AIsOfType is_graph(const ULocale &pLocale=ULocale())
{
    return AIsOfType(UCHAR_POSIX_GRAPH,pLocale);
}

///returns a predicate that returns true if its argument is a lowercase letter
inline AIsOfType is_lower(const ULocale &pLocale=ULocale())
{
    return AIsOfType(UCHAR_LOWERCASE,pLocale);
}

///returns a predicate that returns true if its argument is a printable character
inline AIsOfType is_print(const ULocale &pLocale=ULocale())
{
    return AIsOfType(UCHAR_POSIX_PRINT,pLocale);
}

///returns a predicate that returns true if its argument is a space
inline AIsOfType is_space(const ULocale &pLocale=ULocale())
{
    return AIsOfType(UCHAR_WHITE_SPACE,pLocale);
}

///returns a predicate that returns true if its argument is an uppercase letter
inline AIsOfType is_upper(const ULocale &pLocale=ULocale())
{
    return AIsOfType(UCHAR_UPPERCASE,pLocale);
}

///returns a predicate that returns true if its argument is an hexadecimal digit (typed version)
inline AIsOfType is_xdigit(const ULocale &pLocale=ULocale())
{
    return AIsOfType(UCHAR_POSIX_XDIGIT,pLocale);
}

//@}

///@}

/*namespace fn*/ } /*namespace buola*/ }

#endif
